<?php

/**
 * This is the model class for table "{{cpsupplier}}".
 *
 * The followings are the available columns in table '{{cpsupplier}}':
 * @property string $idcpsupplier
 * @property string $suppliername
 * @property string $address
 * @property string $phone
 * @property string $idswstockgroup
 * @property string $idfdgroup
 * @property integer $flag
 * @property string $category
 *
 * The followings are the available model relations:
 * @property Cpbill[] $cpbills
 * @property Cporder[] $cporders
 * @property Cpquotation[] $cpquotations
 * @property Fdgroup $idfdgroup0
 * @property Swstockgroup $idswstockgroup0
 */
class Cpsupplier extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Cpsupplier the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cpsupplier}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('suppliername, idswstockgroup, category', 'required'),
			array('flag', 'numerical', 'integerOnly'=>true),
			array('suppliername, address, phone', 'length', 'max'=>100),
			array('idswstockgroup, idfdgroup', 'length', 'max'=>10),
			array('category', 'length', 'max'=>5),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idcpsupplier, suppliername, address, phone, idswstockgroup, idfdgroup, flag, category', 'safe', 'on'=>'search'),
                        array('idswstockgroup, idfdgroup', 'validatefkeys'),
		);
	}        
        
        public function validatefkeys($attribute,$params){
               $aux = $attribute . '0';
               if(!$this->$aux)
                    $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}'=>Yii::t('application', $attribute))));
        }      
        
        
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cpbills' => array(self::HAS_MANY, 'Cpbill', 'idcpsupplier'),
			'cporders' => array(self::HAS_MANY, 'Cporder', 'idcpsupplier'),
			'cpquotations' => array(self::HAS_MANY, 'Cpquotation', 'idcpsupplier'),
			'idfdgroup0' => array(self::BELONGS_TO, 'Fdgroup', 'idfdgroup'),
			'idswstockgroup0' => array(self::BELONGS_TO, 'Swstockgroup', 'idswstockgroup'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idcpsupplier' => Yii::t('application', 'Idcpsupplier'),
			'suppliername' => Yii::t('application', 'Suppliername'),
			'address' => Yii::t('application', 'Address'),
			'phone' => Yii::t('application', 'Phone'),
			'idswstockgroup' => Yii::t('application', 'Idswstockgroup'),
			'idfdgroup' => Yii::t('application', 'Idfdgroup'),
			'flag' => Yii::t('application', 'Flag'),
			'category' => Yii::t('application', 'Category'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idcpsupplier',$this->idcpsupplier,true);
		$criteria->compare('suppliername',$this->suppliername,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('idswstockgroup',$this->idswstockgroup,true);
		$criteria->compare('idfdgroup',$this->idfdgroup,true);
		$criteria->compare('flag',$this->flag);
		$criteria->compare('category',$this->category,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}